namespace Spotted.Storage.Queues.Pages
{
    using System;
    using System.Windows;
    using System.Windows.Navigation;

    using Spotted.Storage.Queues.ViewModels;

    public partial class QueueDetailsPage
    {
        public QueueDetailsPage()
        {
            this.InitializeComponent();

            this.ViewModel = new QueueDetailsPageViewModel();
        }

        public QueueDetailsPageViewModel ViewModel
        {
            get { return this.DataContext as QueueDetailsPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string currentQueueName;
            if (!this.NavigationContext.QueryString.TryGetValue("currentQueueName", out currentQueueName))
                this.NavigationService.GoBack();
            else
                this.ViewModel.QueueName = currentQueueName;
        }

        private void OnQueueMessage(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.ViewModel.QueueMessageContent))
                MessageBox.Show("The queue message cannot be null nor empty.", "Queue Message Result", MessageBoxButton.OK);
            else
                this.ViewModel.QueueMessage();
        }

        private void OnDequeueMessage(object sender, RoutedEventArgs e)
        {
            this.ViewModel.DequeueMessage();
        }

        private void OnDeleteQueueMessages(object sender, EventArgs e)
        {
            this.ViewModel.CloudQueueMessages.Clear();
        }
    }
}
